package paperToChaKanChengJi.adapter

import android.app.Activity
import android.view.View
import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.utils.LancooUtils
import kotlinx.android.synthetic.main.item_subject.view.*
import paperToChaKanChengJi.data.TypeScoreRate

/**
 * Created by Dadong on 2020/3/26
 * Describe: 试题分析
 */
class SubjectAdapter(
        val layer: Int,
        val paperId: String,
        val activity: Activity,
        val lifecycle: Lifecycle,
        layoutResId: Int, data: MutableList<TypeScoreRate>
) : BaseQuickAdapter<TypeScoreRate, BaseViewHolder>(layoutResId, data) {
    override fun convert(helper: BaseViewHolder, item: TypeScoreRate?) {
        with(helper.itemView) {
            subject_tv.text = if (item!!.GenreName.isNullOrEmpty()) {
                item.TypeName
            } else {
                item.GenreName
            }
            scoring_tv.text = item.TotalScore
            score_tv.text = item.AvgScore
            if (item.ScoreRate == "--") {
                rate_tv.text = item.ScoreRate
                rate_tv.setTextColor(helper.itemView.resources.getColor(R.color.c666666))
            } else {
                rate_tv.text = "${item.ScoreRate}%"
                when {
                    item.ScoreRate.toDouble() >= 85 -> {
                        rate_tv.setTextColor(helper.itemView.resources.getColor(R.color.c0ACD81))
                    }
                    item.ScoreRate.toDouble() >= 70 -> {
                        rate_tv.setTextColor(helper.itemView.resources.getColor(R.color.c5498FF))
                    }
                    item.ScoreRate.toDouble() >= 60 -> {
                        rate_tv.setTextColor(helper.itemView.resources.getColor(R.color.cFF9137))
                    }
                    else -> {
                        rate_tv.setTextColor(helper.itemView.resources.getColor(R.color.cF97878))
                    }
                }
            }

            duration_tv.text = item.AnswerLength
            subject_cl.setBackgroundColor(this.resources.getColor(R.color.white))
            subject_iv.setImageResource(R.mipmap.icon_down)
            var genreName = item.GenreName
            if(genreName!!.isEmpty()){
                genreName = item.TypeName
            }
            if (!item.QuesScoreRateList.isNullOrEmpty()) {
                val childAdapter = ChildAdapter(genreName, layer, paperId, item.TypeNo, activity, lifecycle, R.layout.item_subject, item.QuesScoreRateList)
                subject_rv.layoutManager = LinearLayoutManager(context)
                subject_rv.adapter = childAdapter
            }

            subject_cl.setOnClickListener {
                if (subject_rv.visibility == View.VISIBLE) {
                    subject_rv.visibility = View.GONE
                    subject_iv.setImageResource(R.mipmap.icon_down)
                    subject_cl.setBackgroundColor(this.resources.getColor(R.color.white))
                } else {
                    subject_rv.visibility = View.VISIBLE
                    subject_iv.setImageResource(R.mipmap.icon_up)
                    subject_cl.setBackgroundColor(this.resources.getColor(R.color.cf2f9ff))
                }
            }
        }
    }
}